﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using wp7cinema.Model;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net;
using System;
using System.Text.RegularExpressions;
using System.Linq;
using System.Text;

namespace wp7cinema.ViewModel
{
    public struct StilCut
    {
        public string ImageSource { get; set; }
    }
    public class StillCutViewModel : ViewModelBase
    {
        public MovieSimpleInfo _movieSimpleInfo;
        public MovieSimpleInfo MovieSimpleInfo
        {
            get { return _movieSimpleInfo; }
            set
            {
                _movieSimpleInfo = value;
                RaisePropertyChanged("MovieSimpleInfo");
            }
        }
        public IEnumerable<StilCut> _photos;
        public IEnumerable<StilCut> Photos
        {
            get { return _photos; }
            set
            {
                _photos = value;
                RaisePropertyChanged("Photos");
            }
        }

        public StillCutViewModel()
        {
            if (IsInDesignMode)
            {
                MovieSimpleInfo = new MovieSimpleInfo
                {
                    Description = "드림머신이라는 기계로 타인의 꿈과 접속해 생각을 빼낼 수 있는 미래사회.‘돔 코브..",
                    DetailViewCode = "52515",
                    Genre = "SF",
                    ImgUrl = "http://imgmovie.naver.com/mdi/mit500/0525/52515_P31_095232.jpg",
                    Point = "9.15",
                    PreorderCode = "52515",
                    ReleaseDay = "2010.07.21",
                    Runtime = "147",
                    ShareRate = "75.5",
                    Title = "인셉션"
                };
                Photos = new ObservableCollection<StilCut> {
                    new StilCut { ImageSource = "http://imgmovie.naver.com/mdi/mit120/0527/52747_P02_115348.jpg" },
                    new StilCut { ImageSource = "http://imgmovie.naver.com/mdi/mit120/0527/52747_S15_123108.jpg" },
                    new StilCut { ImageSource = "http://imgmovie.naver.com/mdi/mit120/0527/52747_S13_123105.jpg" },
                    new StilCut { ImageSource = "http://imgmovie.naver.com/mdi/mit120/0527/52747_C26_123254.jpg" },
                    new StilCut { ImageSource = "http://imgmovie.naver.com/mdi/mit120/0527/52747_S17_123136.jpg" },
                    new StilCut { ImageSource = "http://imgmovie.naver.com/mdi/mit120/0527/52747_S09_123036.jpg" },
                    new StilCut { ImageSource = "http://imgmovie.naver.com/mdi/mit120/0527/52747_S30_172824.jpg" },
                    new StilCut { ImageSource = "http://imgmovie.naver.com/mdi/mit120/0527/52747_S55_095535.jpg" },
                    new StilCut { ImageSource = "http://imgmovie.naver.com/mdi/mit120/0527/52747_S16_123110.jpg" },
                    new StilCut { ImageSource = "http://imgmovie.naver.com/mdi/mit120/0527/52747_S14_123107.jpg" }
                };
            }
            else
            {
                MovieSimpleInfo = App.Current.Resources["MovieSimpleInfo"] as MovieSimpleInfo;

                string url = "http://movie.naver.com/movie/bi/mi/photo.nhn?code=" + MovieSimpleInfo.DetailViewCode;
                WebClient client = new WebClient();
                client.DownloadStringCompleted += StillCutPageDownloadCompleted;
                client.DownloadStringAsync(new Uri(url));
            }
        }

        void StillCutPageDownloadCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            string html = e.Result;
 
            MatchCollection matches = Regex.Matches(html, @"http://imgmovie.*jpg");

            ObservableCollection<StilCut> stilcuts = new ObservableCollection<StilCut>();
            foreach (Match m in matches)
            {
                stilcuts.Add(new StilCut { ImageSource = m.Value });
            }
            Photos = stilcuts;
        }
    }
}